package com.jakewu.horizontalgridview;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.HorizontalGridView;
import android.support.v17.leanback.widget.ItemBridgeAdapter;

import com.jakewu.horizontalgridview.data.Entries;
import com.jakewu.horizontalgridview.data.EntryPresenter;
import com.jakewu.horizontalgridview.utils.EntryLruCache;
import com.jakewu.horizontalgridview.utils.Utils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class MainActivity extends Activity {
    private Picasso mPicasso = null;
    private ArrayList entryList = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.setCardContext(this);
        initData();
        initView();
    }

    private void initData() {

         entryList = new ArrayList();

        for(int i = 0; i< 4 ;i++) {
            entryList.add(new Entries("产品注册","产品注册享福利","file://",""));
        }
    }

    private void initView() {
        HorizontalGridView horizontalGridView = findViewById(R.id.hg_entry);
        horizontalGridView.setHorizontalSpacing(20);
        ArrayObjectAdapter arrayObjectAdapter = new ArrayObjectAdapter(new EntryPresenter());
        ItemBridgeAdapter itemBridgeAdapter = new ItemBridgeAdapter(arrayObjectAdapter);

        horizontalGridView.setAdapter(itemBridgeAdapter);
        arrayObjectAdapter.addAll(0,entryList);
    }

    public Picasso getPicassoInstance() {
        if (mPicasso == null) {
            mPicasso = new Picasso
                    .Builder(this)
                    .memoryCache(new EntryLruCache(this))
                    .build();
        }
        return mPicasso;
    }
}
